package com.jweb.framework.ws.soap.axiom;

import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.transform.Result;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPHeader;
import org.apache.axiom.soap.SOAPHeaderBlock;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapHeaderException;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.transform.StaxResult;
/**
 * 修改人： Thomas
 * 修改内容：新增 
 * 类说明：
 */
abstract class AxiomSoapHeader extends AxiomSoapElement implements SoapHeader{
	
	AxiomSoapHeader(SOAPHeader axiomHeader , SOAPFactory axiomFactory) {
		super(axiomHeader, axiomFactory);
	}
	
	public Result getResult(){
		return new StaxResult(new DXMLStreamWriter(getAxiomHeader(), getAxiomFactory()));
	}
	
	public SoapHeaderElement addHeaderElement(QName name) {
		try {
			OMNamespace namespace = getAxiomFactory().createOMNamespace(name.getNamespaceURI(), QNameUtils.getPrefix(name));
			SOAPHeaderBlock axiomHeaderBlock = getAxiomHeader().addHeaderBlock(name.getLocalPart(), namespace);
			return new AxiomSoapHeaderElement(axiomHeaderBlock , getAxiomFactory());
		} catch (OMException e) {
			throw new AxiomSoapHeaderException(e);
		}
	}
	
	public void removeHeaderElement(QName name) throws SoapHeaderException {
		try {
			OMElement element = getAxiomHeader().getFirstChildWithName(name);
			if(element != null){
				element.detach();
			}
		} catch (OMException e) {
			throw new AxiomSoapHeaderException(e);
		}
	}
	
	public Iterator<SoapHeaderElement> examineMustUnderstandHeaderElements(String role){
		try {
			return new AxiomSoapHeaderElementIterator(getAxiomHeader().examineMustUnderstandHeaderBlocks(role));
		} catch (OMException e) {
			throw new AxiomSoapHeaderException(e);
		}
	}
	
	public Iterator<SoapHeaderElement> examineAllHeaderElements(){
		try {
			return new AxiomSoapHeaderElementIterator(getAxiomHeader().examineAllHeaderBlocks());
		} catch (OMException e) {
			throw new AxiomSoapHeaderException(e);
		}
	}
	
	public Iterator<SoapHeaderElement> examineHeaderElements(QName name) throws SoapHeaderException {
        try {
            return new AxiomSoapHeaderElementIterator(getAxiomHeader().getChildrenWithName(name));
        }
        catch (OMException ex) {
            throw new AxiomSoapHeaderException(ex);
        }
    }
	
	protected SOAPHeader getAxiomHeader(){
		return (SOAPHeader) getAxiomElement();
	}
	
	
	protected class AxiomSoapHeaderElementIterator implements Iterator<SoapHeaderElement> {
		
		private final Iterator<SOAPHeaderBlock> axiomIterator ;
		
		protected AxiomSoapHeaderElementIterator(Iterator<SOAPHeaderBlock> axiomIterator){
			this.axiomIterator = axiomIterator ;
		}
		
		public boolean hasNext(){
			return axiomIterator.hasNext();
		}
		
		public SoapHeaderElement next(){
			try {
				SOAPHeaderBlock axiomHeaderBlock = axiomIterator.next() ;
				return new AxiomSoapHeaderElement(axiomHeaderBlock , getAxiomFactory());
			} catch (OMException e) {
				throw new AxiomSoapHeaderException(e);
			}
		}
		
		public void remove(){
			axiomIterator.remove();
		}
		
		
	}
	

}
